<template>
  <div class="page-search" v-if="isQuery">
    <!-- 搜索模块 -->
    <el-form ref="formRef" :model="formSearch" label-width="80px" size="large">
      <el-row :gutter="20">
        <template v-for="item in searchConfig.searchFormData" :key="item.prop">
          <el-col :span="8">
            <el-form-item :label="item.label" :prop="item.prop">
              <template v-if="item.type === 'input'">
                <el-input v-model="formSearch[item.prop]" :placeholder="item.placeholder" />
              </template>
              <template v-if="item.type === 'date-picker'">
                <el-date-picker
                  v-model="formSearch[item.prop]"
                  type="daterange"
                  range-separator="To"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <!-- 重置和查询 -->
    <div class="btns">
      <el-button size="large" @click="resetClickInfo">重置</el-button>
      <el-button size="large" @click="clickQueryInfo" type="primary">查询</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import useMenuAuthor from '@/hooks/useMenuAuthor'
import type { ElForm } from 'element-plus'
import { reactive, ref } from 'vue'
// 定义自定义事件/接收的属性
interface IProps {
  searchConfig: {
    pageName: string
    labelWidth?: string
    searchFormData: any[]
  }
}
const props = defineProps<IProps>()
// 搜索的权限
const isQuery = useMenuAuthor(`${props.searchConfig.pageName}:query`)
// 定义form的数据
const initValue: any = {}
for (const item of props.searchConfig.searchFormData) {
  initValue[item.prop] = item.initValue ?? ''
}
const formSearch = reactive(initValue)
const emit = defineEmits(['userQueryList', 'resetQueryList'])
const formRef = ref<InstanceType<typeof ElForm>>()
// 重置该表单项，将其值重置为初始值，并移除校验结果
// 利用表单的 resetFields方法 重置信息
function resetClickInfo() {
  formRef.value?.resetFields()
  emit('resetQueryList')
}
// 查询用户列表
function clickQueryInfo() {
  emit('userQueryList', formSearch)
}
</script>

<style lang="less" scoped>
.page-search {
  background-color: var(--el--page--search--bg--);
  border-radius: 10px;
  border: 1px solid var(--el-border-color);
  padding: 20px 20px 20px 0;
}
.btns {
  text-align: right;
}
</style>
